/**
 * @author Don
 */
module zysx {
	export class LayerMgr {
		private static _ins: LayerMgr;
		private root: eui.UILayer;

		private viewlayer: egret.DisplayObjectContainer;
		private poplayer: egret.DisplayObjectContainer;
		private tiplayer: egret.DisplayObjectContainer;

		private tipViews:Array<TipItem>    //飘字提示界面

		private _currentView: BaseView; //当前打开的全屏界面对象

		private views: Array<BaseView>;
		public constructor() {
			this.viewlayer = new egret.DisplayObjectContainer();
			this.poplayer = new egret.DisplayObjectContainer();
			this.tiplayer = new egret.DisplayObjectContainer();
			this.tipViews = [];
		}
		/**
		 * 获取单例
		 */
		public static get ins(): LayerMgr {
			if (!this._ins) this._ins = new LayerMgr();
			return this._ins;
		}
		/**
		 * 初始化单例
		 */
		public init(root: eui.UILayer): void {
			this.root = root;
			this.root.addChild(this.viewlayer);
			this.root.addChild(this.poplayer);
			this.root.addChild(this.tiplayer);
			this.views = [];
		}

		public onResize(): void {
			this.viewlayer.width = zysx.StageMgr.stageWidth;
			this.viewlayer.height = zysx.StageMgr.stageHeight;
			this.poplayer.width = zysx.StageMgr.stageWidth;
			this.poplayer.height = zysx.StageMgr.stageHeight;
			this.tiplayer.width = zysx.StageMgr.stageWidth;
			this.tiplayer.height = zysx.StageMgr.stageHeight;
			for (let i = 0; i < this.views.length; i++) {
				if (this.views[i]["onStageResize"]) this.views[i]["onStageResize"]();
			}
		}
		/**
		 * 通用打开界面接口
		 */
		public openView(viewName: string, data?: any, layerType: number = LayerType.UI, zIndex?: number): zysx.BaseView {
			let contentLayer;
			switch (layerType) {
				case LayerType.UI:
					contentLayer = this.viewlayer;
					break;
				case LayerType.POP:
					contentLayer = this.poplayer;
					break;
				case LayerType.TIP:
					contentLayer = this.tiplayer;
					break;
			}
			let view:zysx.BaseView = new window[viewName](data);
			
			// let view:zysx.BaseView = eval("new " + viewName)
			
			if (!view) return;
			// view._data = data;
			this.views.push(view);

			if (view.resName) {
				this.showConnectView();
				zysx.ResMgr.getInstance().loadGroup(view.resName, function () {
					this.closeConnectView();
					contentLayer.addChild(view);
					if (layerType == LayerType.UI) {
						this.closeCurrentView();
						this._currentView = view;
					}
				}, function (evt: RES.ResourceEvent) {

				}, function () {
					this.closeConnectView();
				}, this, 100);
			} else {
				contentLayer.addChild(view);
				if (layerType == LayerType.UI) {
					this.closeCurrentView();
					this._currentView = view;
				}
			}
			return view;
		}
		public closeCurrentView(): void {
			if (this._currentView) {
				this._currentView.onClose();
				this.removeFromPool(this._currentView);
				this._currentView = null;
			}
		}
		public get currentView(): BaseView {
			return this._currentView;
		}
		//打开加载界面
		public showConnectView(): void {

		}
		public closeConnectView(): void {

		}
		//打开文字提示（改界面用完放入对象池）
		public showTip(mess:string,color:number= 0xff0000):void{
			let view = this.tipViews.pop();
			if(!view) {
				view = new TipItem({mess,color});
			} else {
				view.onOpen({mess,color});
			}
			this.tiplayer.addChild(view);
		}
		public circleTip(item:TipItem):void{
			this.tipViews.push(item);
		}
		//打开确认提示界面
		public showConfirm(mess: string, sureCall: Function = null, cancelCall: Function = null, target: any = null): void {
			this.openView("ConfirmView",{mess,sureCall,cancelCall,target},LayerType.POP);	
		}

		public closeViewByTarget(view: BaseView): void {
			if (view) {
				this.removeFromPool(view);
			}
			view.onClose();
		}
		//关闭所有pop界面
		public closePopView(): void {
			this.poplayer.removeChildren();
			this.views = [];
		}

		private removeFromPool(view: BaseView): void {
			for (let i = 0; i < this.views.length; i++) {
				if (this.views[i] == view) {
					this.views.splice(i, 1);
					break;
				}
			}
		}

		public getComponentGlobalPoint(viewName: string, componentName: string): any {
			let view: BaseView = new window[viewName]();
			if (view) {
				view.validateNow();//没作用，，，
				let component: any = view[componentName];
				if (component.parent) {
					return component.parent.localToGlobal(component.x, component.y)
				} else {
					return component.localToGlobal(component.x, component.y);
				}
			}
			return { x: 0, y: 0 };
		}
	}
}